
import { _decorator, Component, EventKeyboard, Input, input, KeyCode, Node, tween, UITransform, Vec2, Vec3 } from 'cc';
import { TouchCtrl } from '../utils/TouchCtrl';
import { EventLister } from '../utils/EventLister';
import { EventProtoCtrl } from '../utils/EventProtoCtrl';
import { GameCtrl } from '../game/GameCtrl';
import { JoyStick } from '../utils/JoyStick';
import { JoyStickManager } from '../utils/JoyStickManager';
import { CamreaCtrl } from '../game/CamreraCtrl';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = PlayerCtrl
 * DateTime = Sat Feb 24 2024 15:44:36 GMT+0800 (中国标准时间)
 * Author = heyongtao
 * FileBasename = PlayerCtrl.ts
 * FileBasenameNoExtension = PlayerCtrl
 * URL = db://assets/scripts/player/PlayerCtrl.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
@ccclass('PlayerCtrl')
export class PlayerCtrl extends Component {

    private initScalePos : Vec3 = null;
    
    @property(Node)
    particleNode : Node = null;

    onLoad(): void {
        this.initScalePos = this.node.getScale();
    }
    start () {
    }

    update (deltaTime: number) {
        if(JoyStickManager.GetInstance().joyStick.moving)
        {
            let cx = Math.cos(JoyStickManager.GetInstance().joyStick.angle) * JoyStickManager.GetInstance().speed.x;
            let cy = Math.sin(JoyStickManager.GetInstance().joyStick.angle) * JoyStickManager.GetInstance().speed.y;
            // 方向
            let dirX = cx > 0 ? 1 : 0;
            let curScale = this.node.getScale();
            curScale.x = dirX ? this.initScalePos.x : -this.initScalePos.x;
            this.node.setScale(curScale);
            
            // //根据角度移动
            let x = this.node.getPosition().x + cx;
            let y = this.node.getPosition().y + cy;
            if(x > CamreaCtrl.GetInstance().maxX) x = CamreaCtrl.GetInstance().maxX;
            if(x < CamreaCtrl.GetInstance().minX - CamreaCtrl.GetInstance().winX/2) x = CamreaCtrl.GetInstance().minX - CamreaCtrl.GetInstance().winX/2;
            if(y > CamreaCtrl.GetInstance().maxY) y = CamreaCtrl.GetInstance().maxY;
            if(y < CamreaCtrl.GetInstance().minY - CamreaCtrl.GetInstance().winY/2) y = CamreaCtrl.GetInstance().minY - CamreaCtrl.GetInstance().winY/2;
            this.node.setPosition(new Vec3(x, y, 0));
            //console.log("player pos : " ,this.node.getPosition());
            //this.particleNode.setPosition(this.node.getPosition());
        }
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
